<?php
/**
 * Created by PhpStorm.
 * User: serafina
 * Date: 2015/9/23
 * Time: 09:50
 */

function mobile_service_competitors_list($parameters){
    global $user;
    $query = db_select('addr', 'A');//地址表
    $query->join('competitors', 'C', 'A.addr_id=C.addr_id');//内连接 成一个表
    $query->leftJoin('fertilizer', 'F', 'F.fertilizer_id=C.fertilizer_id');
    $query->leftJoin('brand', 'B', 'B.brand_id=C.brand_id');
    $query->fields('C');
    $query->addField('F', 'fertilizer_name');
    $query->addField('B', 'brand_name');
    if(!user_access('view all competitors') && user_access('view own competitors')){
        $query->condition('C.author_uid',$user->uid, '=');
    }
    //地址级别查询
    if (!empty($parameters['province'])) {//省级
        $query->condition('A.province_id', $parameters['province'], '=');
    }
    //模糊条件筛选
    if (!empty($parameters['year'])) {//模糊年度查询条件
        $query->condition('C.date', db_like($parameters['year']) . '-' . '%', 'LIKE');
    }

    $query->leftJoin('addr', 'A1', 'A.province_id=A1.addr_id');
    $query->addField('A1', 'name', 'province');

    $query->orderBy('C.competitors_id', 'DESC');
    if (!empty($parameters['cursor']) && !empty($parameters['comparator'])) { //游标翻页
        $query->condition('C.competitors_id', $parameters['cursor'], $parameters['comparator']);
    }
    $query->range(0,10);

    $results = $query->execute();
    $competitors_list = [];
    if(!empty($results)){
        foreach ($results as $result) {
            $competitors_list[] = $result;
        }
    }
    $object_competitors_list = (object)["data"=>$competitors_list];
    return  $object_competitors_list;
}

function mobile_service_competitors_add($parameters){
    global $user;
    $module_name = "competitors";
    $id = !empty($parameters['competitors_id'])?$parameters['competitors_id']:'';
    if (empty($id)) { //add new brand_sales
        if(!user_access('add '.$module_name)){
            return ['option'=>0,'errorNum'=>3];
        }
        $nid = db_insert('competitors') // Table name no longer needs {}
        ->fields(array(
            'addr_id' => $parameters['addr_id'],
            'fertilizer_id' => $parameters['fertilizer_id'],
            'brand_id' => $parameters['brand_id'],
            'date' => $parameters['date'],
            'n' => $parameters['n'],
            'p' => $parameters['p'],
            'k' => $parameters['k'],
            'npk_total' => $parameters['npk_total'],
            'sm' => $parameters['sm'],
            'factory_price' => $parameters['factory_price'],
            'arrival_price' => $parameters['arrival_price'],
            'retail_price' => $parameters['retail_price'],
            'rebate' => $parameters['rebate'],
            'sales_policy' => $parameters['sales_policy'],
            'author_uid' => $user->uid,
            'timestamp' => time(),))
            ->execute();
            $result_option = (object)["option" => 1];
            return $result_option;
    } else {
        if(!mobile_user_edit_access($module_name,$module_name.'_id',$id,'edit own '.$module_name,'edit all '.$module_name)){
            return ['option'=>0,'errorNum'=>3];
        }
        $nid = db_update('competitors')
        ->fields(array(
            'addr_id' => $parameters['addr_id'],
            'fertilizer_id' => $parameters['fertilizer_id'],
            'brand_id' => $parameters['brand_id'],
            'date' => $parameters['date'],
            'n' => $parameters['n'],
            'p' => $parameters['p'],
            'k' => $parameters['k'],
            'npk_total' => $parameters['npk_total'],
            'sm' => $parameters['sm'],
            'factory_price' => $parameters['factory_price'],
            'arrival_price' => $parameters['arrival_price'],
            'retail_price' => $parameters['retail_price'],
            'rebate' => $parameters['rebate'],
            'sales_policy' => $parameters['sales_policy'],
            'timestamp' => time(),))
            -> condition('competitors_id',$id, '=')
            ->execute();
        if($nid > 0){
            $result_option = (object)["option" => 1];
            return $result_option;
        }
        $result_option = (object)["option" => 0];
        return $result_option;
    }
}